<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/24 0024
 * Time: 15:56
 */

namespace app\index\controller;

use app\index\controller\BaseController;
use app\index\model\P_management;
use app\index\model\Answer_mangement;
use app\index\model\record;
use app\index\model\Topic_management;
use app\index\model\prizes;
use app\index\model\Upfiles;
use think\Db;
use think\Request;

class Form extends BaseController
{
    //答题配置数据
    public function answer(){
        $am = new Answer_mangement();
        $ares = $am->getAmanagement();
        $this -> assign('ares',$ares);
        return $this -> fetch('answer_mangement');
    }

    //答题配置修改
    public function updateanswer(){
        $data= $_POST;
        $am = new Answer_mangement();
        $res =$am-> where('id=1')->setField($data);
        if ($res){
            return 1;
        }else{
            return 0;
        }
    }
    //抽奖配置模板渲染
    public function ldraw(){
				$am = new Answer_mangement();
				$ares = $am->getAmanagement();
				$this -> assign('ares',$ares);
        $data = Db::name('luck_draw')->limit(8)->select();
        $this->assign('data',$data);
        return $this -> fetch('Luck_draw');
    }
    //玩家信息数据
    public function plist(){
        $rresd = Db::connect();
        $shu = $rresd->name('p_management')->count();
            $arraa = $rresd->name('p_management')->order('total_integral desc')->limit(10)->select();

            foreach ($arraa as $key=>$value){
                if($value['friend']){
                    $value['friend'] = json_decode($value['friend'],true);
                    $arr = $rresd->name('p_management')->field('nickname')->where('openid','in',$value['friend'])->select();
                    $arrs = [];
                    foreach ($arr as $values){
                       $arrs[] = $values['nickname'];
                    }
                    $arraa[$key]['friend'] = implode(',',$arrs);
                }
            }
            $this -> assign('res',$arraa);
            $this -> assign('shu',$shu);
            return $this -> fetch('P_management');
    }
    //题库管理模板数据
    public function  tmanaagement(){
        $res = Db::connect();
        $arra = $res->name("topic_management")->limit(10)->select();
        $shu = $res->name("topic_management")->count();
        //获取答案选项的数据
        foreach ($arra as $key=>$value){
            if($value['optional']){
                $arra[$key]['optional']=implode(';',json_decode($value['optional'],true));
            }
            //对答案的数据转化
            if($value['answer']==0){
                $arra[$key]['answer']='A';
            }elseif($value['answer']==1){
                $arra[$key]['answer']='B';
            }elseif($value['answer']==2){
                $arra[$key]['answer']='C';
            }elseif($value['answer']==3){
                $arra[$key]['answer']='D';
            }
        }
        $this ->assign('arra', $arra);
        $this ->assign('shu', $shu);
        return $this->fetch('topic_management');
    }
    //数据添加页面渲染
    public function add(){
        return $this->fetch('add_t');
    }
    //数据添加实现
    public function addt(){
        $tm = new Topic_management();
        $data = $_POST;
        if($data['answer']=='A'){
            $data['answer']=0;
        }elseif($data['answer']=='B'){
            $data['answer']=1;
        }elseif($data['answer']=='C'){
            $data['answer']=2;
        }elseif($data['answer']=='D'){
            $data['answer']=3;
        }
        $res = $tm -> addTmanagement($data);
        if($res){
            return 1;
        }else{
            return 0;
        }
    }
    //答题页面渲染实现
    public function update($id){
        //根据id查询这条数据
        $tm = db('topic_management')->field('id,subject,optional,answer,score')->find($id);
        //将选项数据转化成字符串，展示在前台页面
        $tm['optional']=implode(';',json_decode($tm['optional'],true));
        //处理取出的答案数据
        if($tm['answer']==0){
            $tm['answer']='A';
        }elseif($tm['answer']==1){
            $tm['answer']='B';
        }elseif($tm['answer']==2){
            $tm['answer']='C';
        }elseif($tm['answer']==3){
            $tm['answer']='D';
        }
        $this ->assign('tm',$tm);
        return $this->fetch('update_t');
    }
	
    //答案数据的更新操作
    public function updatet(){
        $tm = new Topic_management();
        $data = $_POST;
        //将更好好的数据转化成json格式存储
        $data['optional'] = json_encode(explode(';',trim($data['optional'],';')));
        //存储答案数据
        if($data['answer']=='A'){
            $data['answer']=0;
        }elseif($data['answer']=='B'){
            $data['answer']=1;
        }elseif($data['answer']=='C'){
            $data['answer']=2;
        }elseif($data['answer']=='D'){
            $data['answer']=3;
        }
        $sava = $tm -> update($data);
        if ($sava){
            return 1;
        }else{
            return 0;
        }
    }
    //答案数据删除操作
    public function dels($id){
       /* Topic_management::destroy($id);*/
        if (Topic_management::destroy($id)){
            return 1;
        }else{
            return 0;
        }

    }
    //兑换奖品页面渲染
    public function prizes(Request $request){
        $p = new prizes();
        $rest = $p->getPrizes($request);
        $this->assign('rest',$rest[1]);
        $this->assign('shu',$rest[0]);
        return $this->fetch('Prizes');
    }
    //兑换奖品添加页面渲染
    public function addp(){
        return $this->fetch('add_p');
    }
    //兑换奖品添加数据
    public function  addj(){
        $data = $_POST;
        $file =$_FILES;
        $addp = Upfiles::Send(ROOT_PATH.'public/static/img',$file,['png','jpeg','bmp']);
        if($addp){
            $data['p_picture'] = $addp[0];
        }
        $add = Db::name('prizes')->insert($data);
        if ($add){
            return 1;
        }else{
            return '添加失败';
        }

      /*  return json($data);*/
    }
    public function record(){
        $r = new record();
        $resul = Db::connect();
        $shu = $resul->name('record')->count();
        $records = $resul->name('record')->order('time desc')->limit(10)->select();
        $this ->assign('records',$records);
        $this ->assign('shu',$shu);
        return $this-> fetch('record');
    }
		public function dao(){
			$data= $_POST;
			$am = new Answer_mangement();
			$res =$am-> where('id=1')->setField($data);
		 if ($res){
					return 1;
			}else{
					return 0;
			}
		}
}